package com.peng.dian.controller;



import com.peng.dian.mapper.DeptMapper;
import com.peng.dian.mapper.UserLogin;
import com.peng.dian.pojo.*;
import com.peng.dian.service.DeptService;
import com.peng.dian.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Collection;
import java.util.List;

@Controller
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;


    @Autowired
    DeptMapper deptMapper;
    @Autowired
    UserLogin userLogin;

    @Autowired
    DeptService deptService;
    //do
    @RequestMapping("/emps")
    public String list(Model model){

        List<Employee2> employee2s = employeeService.queryEmployee2();
        model.addAttribute("emps",employee2s);
        return "emp/list";
    }
    //do
    @GetMapping("/emp")
    public String toAddpage(Model model){
        //查出所有部门的信息

        List<Department1> department1s = deptMapper.queryDeptList();
        model.addAttribute("depaerments",department1s);
        return "emp/add";
    }
    //do
    @PostMapping("/emp")
    public String addEum(Employee1 employee){
        //添加的操作 forward
        employeeService.addEmployee(employee);//调用底层的业务 方法保存员工的信息
        return "redirect:/emps";
    }


    //去员工的修改界面
    @GetMapping("/emp/{id}")
    public String toUpdateEmp(@PathVariable("id")Integer id, Model model){
        //查出原来的数据
        Employee1 employee1 = employeeService.queryEmployeeById(id);
        model.addAttribute("emp",employee1);
        List<Department1> department1s = deptMapper.queryDeptList();
        model.addAttribute("depaerments",department1s);
        return "emp/update";
    }
    //员工信息修改
    @RequestMapping("/updateEmp")
    public String fromUpdateToIndex(Employee1 employee){
        employeeService.updateEmployee(employee);
        return "redirect:/emps";
    }
    //do
    //员工信息删除
    @RequestMapping("/delete/{id}")
    public String deleteEmp( @PathVariable("id") Integer id){

        employeeService.deleteEmployee(id);
        return "redirect:/emps";
    }

    //注册账号的
    @RequestMapping("/register")
    public String registerAccountToPage(@RequestParam("username") String username, @RequestParam("password") String password,
                                        @RequestParam("passwordepear")String passwordagain,
                                        Model model
                                        ){
        if (!password.equals(passwordagain)){
            model.addAttribute("msg","注册失败！！！！！！！,两次密码不一致");
            return "registerpage";
        }else {
            User1 user1=new User1(username,password);
            userLogin.addUser(user1);
            model.addAttribute("msg","注册成功！");
            return "index";
        }
    }

    @PostMapping("/name1")
    public String SelectEum(@RequestParam("name")String name,Model model){
        //添加的操作 forward
        Employee2 employee2 = employeeService.queryEmployeeName(name);
        model.addAttribute("emp",employee2);
        return "emp/accurateselect";
    }
    @PostMapping("/name2")
    public String SelectByDepartmentAllEmploymee(@RequestParam("deptName")String deptName,Model model){
        //添加的操作 forward
        Department2 departmentPeople = deptService.getDepartmentPeople(deptName);
        model.addAttribute("emps",departmentPeople);
        return "emp/selectByDepartment";
    }


}
